<template>

    <UxDialog :visible="visible" fullScreen @close="close()">

        <div class="emb-edit-box">

            <div class="emb-edit-view">
                <DesignerEmbEditPreview v-model="active_color" :stitches="stitches"
                                        :colors="line_hover || line_color" />
                <p>
                    <UxTemplateInputInline v-if="scenes == 'files'" v-model="dst_name" />
                    <strong v-else-if="dst_name">{{ dst_name }}</strong>
                    <span>Size: {{ getIn(dst_width) }}″×{{ getIn(dst_height) }}″</span>
                    <span>Stitches: {{ stitches_count }}</span>
                </p>
            </div>

            <div class="emb-edit-colors">


                <p v-if="scenes == 'files' && changeDst">
                    Modifying thread colors will not affect existing product templates.
                </p>


                <div class="emb-edit-scroller">
                    <div class="emb-edit-inner">
                        <h3>Thread colors</h3>
                        <ul>
                            <li v-for="(color, index) in colorsFormat" :key="index"
                                :class="{ active: index == color_popper }">
                                <b @click="color_popper = color_popper == index ? -1 : index"
                                   @mouseenter="onMouseenter($event, index)" @mouseleave="onMouseleave($event, index)">
                                    <i v-text="index + 1"></i>
                                    <em :style="{ backgroundColor: color.hex }"></em>
                                    <span v-text="color.name"></span>
                                    <svg-icon class="emb-txt-colors-arrow" name="tree-down" />
                                </b>

                                <cite>
                                    <span v-for="item in color_list" :key="item.code"
                                          :class="{ active: item.code == color.code }" @click="setColor(index, item)"
                                          @mouseenter="onPopperEnter(index, item)"
                                          @mouseleave="onPopperLeave(index, item)">
                                        <em :style="{ backgroundColor: item.hex }"></em>
                                        <svg-icon name="checkbox-active-shadow" />
                                        <p class="hover-tip">{{ item.name }}</p>
                                    </span>
                                </cite>
                            </li>
                        </ul>
                    </div>

                </div>


                <div class="emb-edit-save" v-if="type == 'upload' ? true : changeDst">
                    <button type="primary" @click="doConfirm()">Save</button>
                </div>
            </div>


        </div>

    </UxDialog>

</template>

<script>
import { mapGetters } from "vuex";
import DesignUtils from "@/utils/DesignUtils.js";
import { colors, Pattern } from "@/utils/EmbroideryPattern.js";
import { getStitchesByBase64 } from "@/utils/EmbroideryDst.js";

export default {
    props: {
        scenes: { type: String, default: () => '' }
    },
    data () {
        return {
            visible: false,
            stitches: '',
            stitches_count: 0,
            line_color: '',
            line_hover: '',
            dst_name: '',
            dst_width: 0,
            dst_height: 0,
            callback: null,

            color_list: colors,
            active_color: -1,
            color_popper: -1,
            cache_dst: "",
            type: "edit",
        };
    },

    computed: {
        ...mapGetters("designer", {
            platens: "getPlatens",
            pieces: "getPieces",
            activePiece: "getActivePiece",
            activeLayer: "getActiveLayer",
        }),


        colorsFormat () {
            const codes = this.line_color.split(',').filter(i => i)
            return codes.map(i => colors.find(c => c.code == i)).filter(i => i)
        },

        changeDst () {
            return Object.keys(this.cache_dst).some(key => this.cache_dst[key] !== this[key]);
        }

    },
    methods: {
        getIn (val) {
            return (val * 0.1 * 0.0394).toFixed(1)
        },

        setColor (index, color) {
            const val = this.colorsFormat.slice(0)
            val[index] = color
            this.line_color = val.map(i => i.code).join(',')
            this.color_popper = -1;
        },

        // 进入
        onPopperEnter (index, color) {
            const val = this.colorsFormat.slice(0)
            val[index] = color
            setTimeout(() => this.line_hover = val.map(i => i.code).join(','), 0);
        },

        // 离开
        onPopperLeave (index, color) {
            this.line_hover = ''
        },

        // 打开窗口
        openDialog (option) {
            this.visible = true;
            this.stitches = option.stitches;
            this.stitches_count = option.stitches_count;
            this.line_color = option.line_color;
            this.dst_name = option.dst_name;
            this.dst_width = option.dst_width;
            this.dst_height = option.dst_height;
            this.type = option.type ?? 'edit';
            // 设置缓存
            this.cache_dst = {
                line_color: this.line_color,
                dst_name: this.dst_name
            }

            this.callback = option.callback;
        },

        doConfirm () {
            this.callback && this.callback({
                stitches: this.stitches,
                stitches_count: this.stitches_count,
                line_color: this.line_color,
                dst_name: this.dst_name,
                dst_width: this.dst_width,
                dst_height: this.dst_height,
            })
            this.visible = false;
        },
        // 关闭弹窗
        close () {
            this.visible = false;
        },

        onMouseenter (event, index) {
            if (event.target.tagName == 'B') {
                this.active_color = index;
            }
        },

        onMouseleave (event, index) {
            if (event.target.tagName == 'B') {
                this.active_color = -1;
            }
        },
        onClickOutside (event) {
            const outer = {
                B: true,
                CITE: true
            }
            if (!outer[event.target.tagName]) {
                this.color_popper = -1;
            }
        }
    },

    created () {
        this.$on("global:DESIGN_OPEN_EMB_DST_EDIT_DIALOG", (dst) => this.openDialog(dst));
    },
    mounted () {
        document.addEventListener("click", this.onClickOutside);
    },
    beforeDestroy () {
        document.removeEventListener("click", this.onClickOutside);
    }

};
</script>

<style lang="scss" scoped>
.emb-edit-box {
    display: flex;
    height: 100vh;

    .emb-edit-view {
        width: 56%;
        display: flex;
        align-items: center;
        justify-content: center;
        position: relative;
        background: #fafafa;
        overflow: hidden;

        &>p {
            display: flex;
            flex-direction: column;
            position: absolute;
            left: 20px;
            top: 20px;
            right: 20px;

            .input-inline {
                font-family: Roboto-Medium, Roboto;
                font-weight: 500;
                font-size: 20px;
                color: #292929;
                line-height: 24px;
                text-align: left;
                font-style: normal;
                justify-content: flex-start;
                border: none;
                background-color: transparent !important;
                margin-bottom: 6px;

                &::v-deep .input-inline-icon {
                    margin-top: 2px;

                    svg {
                        width: 20px;
                        height: 20px;
                    }
                }
            }

            strong {
                font-family: Roboto-Medium, Roboto;
                font-weight: 500;
                font-size: 20px;
                color: #292929;
                line-height: 24px;
                text-align: left;
                font-style: normal;
                margin-bottom: 6px;
            }

            span {
                font-family: Roboto, Roboto;
                font-weight: 400;
                font-size: 14px;
                color: #555555;
                line-height: 22px;
                text-align: left;
                font-style: normal;
            }
        }
    }

    .emb-edit-colors {
        width: 44%;
        min-width: 572px;
        position: relative;
        box-sizing: border-box;
        display: flex;
        flex-direction: column;
        border-left: 1px solid #ebebeb;
        overflow: hidden;

        .emb-edit-scroller {
            overflow: auto;
            flex: 1;
            padding-top: 32px;
            padding-bottom: 140px;

            &::before {
                content: '';
                display: inline-block;
                width: 0;
                height: 100%;
                vertical-align: middle;
            }

            .emb-edit-inner {
                display: inline-block;
                vertical-align: middle;
            }
        }

        h3 {
            width: 70%;
            margin: 0 auto 0;
            font-family: Roboto-Medium, Roboto;
            font-weight: 500;
            font-size: 28px;
            color: #000000;
            line-height: 32px;
            min-height: 32px;
            text-align: left;
            font-style: normal;
        }

        ul {
            width: 70%;
            margin: 20px auto 0;
            display: flex;
            flex-wrap: wrap;

            li {
                margin: 0 4px 8px;
                width: 180px;
                height: 40px;
                box-sizing: border-box;
                background: #ffffff;
                position: relative;
                border-radius: 8px;

                &::before {
                    content: '';
                    position: absolute;
                    left: 0;
                    top: 0;
                    bottom: 0;
                    right: 0;
                    border: 1px solid #e6e6e6;
                    border-radius: 8px;
                    z-index: 0;
                }

                &>b {
                    position: relative;
                    display: inline-block;
                    width: 100%;
                    height: 100%;
                    display: flex;
                    align-items: center;
                    z-index: 1;
                    cursor: pointer;

                    &>* {
                        pointer-events: none;
                    }

                    &>i {
                        min-width: 24px;
                        text-align: center;
                        font-family: Roboto, Roboto;
                        font-weight: 500;
                        font-size: 12px;
                        color: #757575;
                        line-height: 16px;
                        font-style: normal;
                    }

                    &>em {
                        display: inline-block;
                        width: 20px;
                        height: 20px;
                        background: #fafafa;
                        border-radius: 2px;
                        border: 1px solid rgba(0, 0, 0, 0.2);
                        box-sizing: border-box;
                    }

                    &>span {
                        font-family: Roboto, Roboto;
                        font-weight: 400;
                        font-size: 14px;
                        color: #000000;
                        line-height: 16px;
                        font-style: normal;
                        margin-left: 4px;
                    }

                    &>.emb-txt-colors-arrow {
                        width: 16px;
                        height: 16px;
                        box-sizing: border-box;
                        padding: 2px;
                        margin: 0 10px 0 auto;
                    }
                }

                &>cite {
                    position: absolute;
                    left: 0;
                    right: 0;
                    top: calc(100% + 4px);
                    background: #ffffff;
                    box-shadow: 0px 6px 12px 0px rgba(0, 0, 0, 0.12);
                    border-radius: 8px;
                    display: flex;
                    flex-wrap: wrap;
                    padding: 5px;
                    opacity: 0;
                    pointer-events: none;
                    z-index: 2;

                    &::before {
                        content: '';
                        position: absolute;
                        left: 0;
                        top: 0;
                        bottom: 0;
                        right: 0;
                        border: 1px solid #ebebeb;
                        border-radius: 8px;
                    }

                    span {
                        width: 24px;
                        height: 24px;
                        margin: 5px;
                        background: #ffffff;
                        border-radius: 2px;
                        border: 1px solid rgba(0, 0, 0, 0.2);
                        box-sizing: border-box;
                        position: relative;
                        cursor: pointer;
                    }

                    em {
                        position: absolute;
                        left: 0;
                        top: 0;
                        width: 100%;
                        height: 100%;
                    }

                    svg {
                        position: absolute;
                        left: 50%;
                        top: 50%;
                        transform: translate(-50%, -50%);
                        width: 14px;
                        height: 14px;
                        fill: #fff;
                        display: none;
                    }

                    span.active svg {
                        display: block;
                    }
                }

                &:not(.active):hover {
                    background-color: #f6f6f6;

                    &>.emb-txt-colors-remove {
                        display: block;
                    }

                    &>.hover-tip {
                        display: block;
                    }
                }

                &.active {
                    .emb-txt-colors-arrow {
                        transform: rotate(180deg);
                    }

                    cite {
                        opacity: 1;
                        pointer-events: all;

                        &>span:hover {
                            &>.hover-tip {
                                display: block;
                            }
                        }
                    }
                }

                .hover-tip {
                    font-family: Roboto, Roboto;
                    font-weight: 400;
                    font-size: 14px;
                    color: #fff;
                    line-height: 16px;
                    font-style: normal;
                    position: absolute;
                    background-color: #212121;
                    border-radius: 4px;
                    left: 50%;
                    transform: translateX(-50%);
                    bottom: 100%;
                    margin-bottom: 8px;
                    line-height: 32px;
                    padding: 0 12px;
                    display: none;
                    white-space: nowrap;

                    &::before {
                        content: '';
                        position: absolute;
                        left: 50%;
                        transform: translateX(-50%);
                        top: 100%;
                        width: 0;
                        height: 0;
                        border-top: 3px solid #212121;
                        border-left: 3px solid transparent;
                        border-right: 3px solid transparent;
                    }
                }
            }
        }

        &>p {
            height: 40px;
            background: #edf8fc;
            border-radius: 8px;
            border: 1px solid rgba(8, 113, 150, 0.14);
            line-height: 40px;
            text-align: center;
            position: absolute;
            bottom: 86px;
            left: 50%;
            transform: translateX(-50%);
            max-width: 90%;
            box-sizing: border-box;
            overflow: hidden;
            white-space: nowrap;
            padding: 0 75px;
            font-family: Roboto, Roboto;
            font-weight: 400;
            font-size: 14px;
            color: #087196;
            z-index: 2;
            text-overflow: ellipsis;


        }

        .emb-edit-save {
            position: absolute;
            left: 0;
            bottom: 0;
            right: 0;
            border-top: 1px solid #ebebeb;
            left: 0;
            bottom: 0;
            right: 0;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            z-index: 1;
            background-color: #fff;

            button {
                width: 288px;
                height: 40px;
            }
        }
    }
}
</style>
